#!/bin/bash
# Program:
#   remove shell script for client uninstall
# Author:
#   Waue, Shunfa, Rock {waue, shunfa, rock}@nchc.org.tw
# Version:
#    1.0
# History:
#    


# 需要 master_install 設定的參數區
# Master IP here
Master_IP_Address=your.master.ip.here
# Master Hostname here
Master_Hostname=input_your_master_hostname
nutch_PATH=/home/nutchuser/nutchez/system


[ "`id -u`" != "0" ] && echo "plaese enter password of $USER:"  && exec sudo su -c "$0" "$@"

##########  echo function  ##########
function debug_info () {
  if [ $? -eq 0 ]; then
    echo -e "\033[1;35;40m info - $1 \033[0m"
  fi
}


function show_info () {
  if [ $? -eq 0 ]; then
    echo -e "\033[1;32;40m $1 \033[0m"
  fi
}
##########end echo function ##########

function load_default_lang(){

lang=$(locale | grep 'LANG=' | cut -d "=" -f2)

# Default: source english
. $nutch_PATH/lang/lang_en_US
# if locale is zh then source chinese

echo $lang | grep 'zh' >> /dev/null && source $nutch_PATH/lang/lang_zh_TW
}



# check root
function check_root(){
  debug_info "check_root"
  if [ $USER != "root" ]; then
    show_info "$CR_check_root_1" # "請切換成 root 身份執行移除程式!!!"
    exit
  fi
  show_info "$CR_check_root_2" # "已確認為root身份, 將繼續執行此移除程式！"
}

# shutdown service
function shutdown_service () {
  show_info "$CR_shutdown_service_echo_1" # "關閉本機服務..."
  su nutchuser -c "/opt/nutchez/nutch/bin/hadoop-daemon.sh stop datanode"
  su nutchuser -c "/opt/nutchez/nutch/bin/hadoop-daemon.sh stop tasktracker"
  show_info "$CR_shutdown_service_echo_2" # "本機服務已關閉"
}

# 移除檔案及資料夾
function remove_folders () {
  show_info "$CR_remove_folders_echo_1" # "正在刪除安裝時所建立的檔案及資料夾..."
  rm /usr/local/bin/nutchez_remove
  rm -rf /opt/nutchez
  rm -rf /var/lib/nutchez
  rm -rf /var/log/nutchez/
  rm -rf /tmp/*nutchuser*.pid
  show_info "$CR_remove_folders_echo_2" # "安裝時所建立的檔案及資料夾已刪除"
}


# 還原/etc/hosts
function edit_hosts () {
  show_info "$CR_edit_hosts_echo_1" # "修改/etc/hosts..."
  if [ -e /home/nutchuser/nutchez/system/hosts.bak ];then 
    rm /etc/hosts 
    cp /home/nutchuser/nutchez/system/hosts.bak /etc/hosts
    show_info "$CR_edit_hosts_echo_2" # "完成修改/etc/hosts"
  elif [ -e /home/nutchuser/nutchez/system/hosts ];then
    rm /etc/hosts
    cp /home/nutchuser/nutchez/system/hosts /etc/hosts
    show_info "still using the /etc/hosts created by nutchez "
  elif [ -f /etc/hosts ];then
    show_info "/etc/hosts without recovered!!!"
  else
    show_info " bug ! /etc/hosts error !!!"
  fi
}

function edit_remove_list(){
# 透過scp master上的檔案判斷是否有網路or master是否有開啟
  scp -o StrictHostKeyChecking=no /etc/hosts nutchuser@$Master_IP_Address:/home/nutchuser/nutchez/system/go.sh /home/nutchuser/nutchez/system/ 
  
  if [ -e "/home/nutchuser/nutchez/system/go.sh" ] ;then
    IP_Address=`/sbin/ifconfig eth0 | grep 'inet addr' |  sed 's/^.*addr://g' | sed 's/Bcast.*$//g' | sed 's/ .*// '`
    su nutchuser -c "ssh -o StrictHostKeyChecking=no $Master_IP_Address echo $IP_Address \>\> ~/nutchez/system/remove_list"
    remove_flag=0
  elif [ ! -e "/home/nutchuser/nutchez/system/go.sh" ] ;then
    remove_flag=1
  fi
}


# 移除使用者
function user_delete () {
  show_info "$CR_user_delete_echo_1" # "正在刪除nutchuser使用者..."
  userdel -rf nutchuser >/dev/null 2>&1
  show_info "$CR_user_delete_echo_2" # "使用者nutchuser已刪除"
}

# Main function
function main () {
  load_default_lang
  check_root
  show_info "$CR_main_echo_1" #"警告 - 此一程式為移除此用戶端的nutch node, 若您為誤執行此一程式, 請按Ctrl+c離開此程序！"
  show_info "$CR_main_echo_2" #"本執行程序將會移除安裝nutchEZ時所新增的檔案及nutchuser使用者帳號"
# 詢問是否繼續
  read -p "$CR_main_echo_3" confirm # "請問是否確定移除此一節點：1.確定 2.取消"
# 確認移除
  if [ $confirm -eq 1 ]; then
    shutdown_service
    remove_folders
    edit_hosts
    edit_remove_list
    user_delete
    show_info "$CR_main_echo_4" # "移除程序已完成！"
  elif [ $confirm -eq 2 ]; then
    show_info "$CR_main_echo_5" # "您已取消移除程序！"
    show_info "$CR_main_echo_6" # "若要移除請再重新執行！"
  fi
  
  if [ $remove_flag -eq 1 ]; then
    show_info "You need to edit /home/nutcher/nutchez/system/nutch_nodes and hosts at host $Master_IP_Address due to you lost connect."
  fi
}

if [ -e "/home/nutchuser/nutchez" ]; then
  main
else
  show_info "Installation is not complete!"
  show_info "Remove forcibly[1=agree/2=exit]"
  read -p "[1/2]: " force_do_remove
  if [ $force_do_remove -eq 1 ];then
    main
  else
    exit
  fi
fi
